package com.jackrain.nea.ext.sequence.observer;

import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.config.Resources;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import java.util.Locale;
import java.util.Observable;

/**
 * @author xubin
 * 2017-11-18
 */
@Slf4j
public class SequenceExec extends Observable {

    private static final Locale DEFAULT_LOCALE = Locale.CHINESE;


    public void exec() {
        setChanged();
        notifyObservers();
    }

    /**
     *
     * @param sequenceName
     * @param object
     * @param id
     * @param mapperBean
     * @param locale
     * @param isTest
     * @return
     */
    public SequenceExec add(String sequenceName, JSONObject object, Long id, Object mapperBean,String methodName, Locale locale, boolean isTest) {
        validateParam(sequenceName, id);
        SequenceObserver observer = new SequenceObserver(this, sequenceName, object,  id,null,null, mapperBean,methodName, locale, isTest);
        return this;
    }

    /**
     *
     * @param sequenceName
     * @param object
     * @param id
     * @param mapperBean
     * @param locale
     * @return
     */
    public SequenceExec add(String sequenceName, JSONObject object, Long id, Object mapperBean,String methodName, Locale locale) {
        return add(sequenceName, object,  id, mapperBean,methodName, locale, false);
    }

    /**
     *
     * @param sequenceName
     * @param object
     * @param id
     * @param mapperBean
     * @param isTest
     * @return
     */
    public SequenceExec add(String sequenceName, JSONObject object, Long id, Object mapperBean,String methodName, boolean isTest) {
        return add(sequenceName, object,  id, mapperBean,methodName, DEFAULT_LOCALE, isTest);
    }

    /**
     *
     * @param sequenceName
     * @param object
     * @param id
     * @param mapperBean
     * @return
     */
    public SequenceExec add(String sequenceName, JSONObject object, Long id, Object mapperBean,String methodName) {
        return add(sequenceName, object,  id, mapperBean,methodName, DEFAULT_LOCALE, false);
    }

    /**
     *
     * @param sequenceName
     * @param object
     * @param id
     * @param mapperBean
     * @param locale
     * @param isTest
     * @return
     */
    public SequenceExec add(String sequenceName, JSONObject object, Long id,Class[] extendTypes,Object[] extendParams, Object mapperBean,String methodName, Locale locale, boolean isTest) {
        validateParam(sequenceName, id);
        SequenceObserver observer = new SequenceObserver(this, sequenceName, object,id,extendTypes,extendParams, mapperBean,methodName, locale, isTest);
        return this;
    }

    /**
     *
     * @param sequenceName
     * @param object
     * @param id
     * @param mapperBean
     * @param locale
     * @return
     */
    public SequenceExec add(String sequenceName, JSONObject object, Long id,Class[] extendTypes,Object[] extendParams,  Object mapperBean,String methodName, Locale locale) {
        return add(sequenceName, object,  id,extendTypes,extendParams, mapperBean,methodName, locale, false);
    }

    /**
     *
     * @param sequenceName
     * @param object
     * @param id
     * @param mapperBean
     * @param isTest
     * @return
     */
    public SequenceExec add(String sequenceName, JSONObject object, Long id,Class[] extendTypes,Object[] extendParams,  Object mapperBean,String methodName, boolean isTest) {
        return add(sequenceName, object,  id,extendTypes,extendParams, mapperBean,methodName, DEFAULT_LOCALE, isTest);
    }

    /**
     *
     * @param sequenceName
     * @param object
     * @param id
     * @param mapperBean
     * @return
     */
    public SequenceExec add(String sequenceName, JSONObject object, Long id,Class[] extendTypes,Object[] extendParams,  Object mapperBean,String methodName) {
        return add(sequenceName, object,  id,extendTypes,extendParams, mapperBean,methodName, DEFAULT_LOCALE, false);
    }

    private static void validateParam(String sequenceName, Long id) {
        assert StringUtils.isNotBlank(sequenceName) : Resources.getMessage("序号生成器名称不能为空");
        Assert.notNull(id, Resources.getMessage("ID不能为空"));
        assert id > 0 : Resources.getMessage("ID不能小于等于0");
//        assert StringUtils.isNotBlank(sql) : Resources.getMessage("sql语句不能为空");
    }
}
